// 获取sequelize 实例
const { sequelize } = require("../model/connect");
const { createUserInfo } = require("./userInfoController");
// 传入数据模型函数,并的到 User 模型
const { User } = require("../model/models")(sequelize);
const jwt = require("jsonwebtoken");
const secret = process.env.SECRET_KEY;

const validateUsername = (username) =>
    /^[a-zA-Z0-9\u4e00-\u9fa5]{6,}$/.test(username);
const register = async (ctx) => {
    const { username, password } = ctx.request.body;
    if (!username || !password) {
        ctx.status = 400;
        ctx.body = {
            code: 10001,
            message: "用户名或者密码不能为空",
            data: null,
        };
        return;
    }
    if (password.length < 6) {
        ctx.status = 400;
        ctx.body = {
            code: 10002,
            message: "密码长度不能小于6位",
            data: null,
        };
        return;
    }

    if (!validateUsername(username)) {
        ctx.status = 400;
        ctx.body = {
            code: 10003,
            message: "用户名必须是中英文和数字组成，且不能少于6位",
            data: null,
        };
        return;
    }
    try {
        const existUser = await User.findOne({ where: { username } });
        if (existUser) {
            ctx.status = 400;
            ctx.body = {
                code: 10004,
                message: "用户名已存在",
                data: null,
            };
            return;
        }
        const newUser = await User.create({ username, password });
        await createUserInfo(newUser);
        ctx.body = {
            code: 0,
            message: "注册成功",
            data: newUser,
        };
    } catch (error) {
        ctx.status = 500;
        ctx.body = {
            code: 10005,
            message: "服务器错误",
            data: error.message,
        };
    }
};

const login = async (ctx) => {
    const { username, password } = ctx.request.body;
    if (!username || !password) {
        ctx.status = 400;
        ctx.body = {
            code: 10001,
            message: "用户名或者密码不能为空",
            data: null,
        };
        return;
    }
    if (password.length < 6) {
        ctx.status = 400;
        ctx.body = {
            code: 10002,
            message: "密码长度不能小于6位",
            data: null,
        };
        return;
    }

    if (!validateUsername(username)) {
        ctx.status = 400;
        ctx.body = {
            code: 10003,
            message: "用户名必须是中英文和数字组成，且不能少于6位",
            data: null,
        };
        return;
    }
    try {
        const user = await User.findOne({ where: { username, password } });
        if (!user) {
            ctx.status = 400;
            ctx.body = {
                code: 10006,
                message: "用户名或密码不正确",
                data: null,
            };
            return;
        }
        // 登陆成功,服务端生成密钥
        let token = jwt.sign({ username: user.username, id: user.id }, secret, {
            expiresIn: "1h",
        });
        ctx.status = 200;
        ctx.body = {
            code: 10000,
            message: "登陆成功",
            data: {
                ...user.dataValues,
                token,
            },
        };
    } catch (error) {
        ctx.status = 500;
        ctx.body = {
            code: 10005,
            message: "服务器错误",
            data: error.message,
        };
    }
};

module.exports = {
    register,
    login,
};
